package src.com.qf.controller;

import src.com.qf.entity.Order;
import src.com.qf.entity.User;
import src.com.qf.service.OrderService;
import src.com.qf.service.impl.OrderServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;

@WebServlet("/order")
public class OrderController extends BaseController {
    private OrderService service = new OrderServiceImpl();
    /**
     * 生成订单
     * @param request
     * @param response
     * @return
     */
    public String addOrder(HttpServletRequest request, HttpServletResponse response){
        //地址id
        String strAid = request.getParameter("aid");
        //商品总金额
        String strSum = request.getParameter("sum");
        User user = (User) request.getSession().getAttribute("loginUser");
        Order order = new Order();

        order.setOrderSum(new BigDecimal(strSum));
        order.setAid(Integer.parseInt(strAid));
        order.setUid(user.getId());

        int result = service.addOrder(order);

        if (result == 1){
            return "redirect:orderSuccess.jsp";
        }else {
            request.setAttribute("msg","生成订单失败");
            return "forward:error/error.jsp";
        }

    }

    /**
     * 获取订单列表
     * @param request
     * @param response
     * @return
     */
    public String getOrderList(HttpServletRequest request, HttpServletResponse response){
        User user = (User) request.getSession().getAttribute("loginUser");
        List<Order> list = service.getOrderList(user.getId());
        request.setAttribute("orderList",list);
        return "forward:orderList.jsp";
    }

    /**
     * 查看订单详情
     * @param request
     * @param response
     * @return
     */
    public String getOrderDetail(HttpServletRequest request, HttpServletResponse response){
        String strOid = request.getParameter("oid");
        User user = (User) request.getSession().getAttribute("loginUser");

        Order order = service.getOrderDetail(Integer.parseInt(strOid));
        request.setAttribute("order",order);

        return "forward:orderDetail.jsp";
    }
}
